/*
 * 
 *方法的整理与重构
 *
 *
 */
//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','examInfo');

/*controller部分*/
//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){
	//
	//****与分页相关的****
	//	
	//从接口中获取总的分页数量
	$scope.$on('$getPageCountFromApi',function(event,data){
		$scope.$broadcast('$getPageCount',data);
	});
	
	//捕获分页点击事件并传递currentPage信息到主controller
	 $scope.$on('$pageAction',function(event,data){
		 $scope.$broadcast('$pageChanged',data);
	 });
})


//主要操作controller
authApp.controller("examInfoList",function($scope,$http,Upload,$timeout,$sce){
	//针对此页面需要的参数 should edit
	$scope.isAddM=true;
	$scope.isUrl=true;
	$scope.isHExpert=true;
	$scope.isKemu=true;
	//$scope.isSel01=true;
	$scope.isSel02=true;
	$scope.isSel03=true;
	$scope.isSel04=true;
	$scope.isShuoke=false;
	$scope.isSkill=false;
	$scope.majorId="";
	
	//**********主体方法
	//设置分页初始参数（最好与$scope.currentPage和$scope.itemsPerPage一致！！！！//这边之所以$scope.pageNumber=0是因为后台接口第一页为0）
	$scope.ifFirstTimeQuery=true;
	$scope.pageNumber=0;
	$scope.pageSize=10;
	var queryMainApi=apis.queryAuthExamPage_review;//！！！should edit
	$scope.queryMain=function(){
		$http({
			method:'post',
			url:queryMainApi,
			data:{
				'pageNumber':$scope.pageNumber,
				'pageSize':$scope.pageSize,
				'subject':$scope.subject01,
				'taskId':$scope.taskId01,
				'major':$scope.major01
				}
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
				//将返回的pagecount和current传到上面的controller
				$scope.$emit('$getPageCountFromApi',response.data);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	//捕捉分页点击事件并做处理
	$scope.$on("$pageChanged",function(event,data){
		if(!$scope.ifFirstTimeQuery){//解决第一次会调用两次接口的问题
			$scope.pageNumber=data.currentPage-1;//这边之所以-1是因为后台接口第一页为0
			$scope.pageSize = 10;
			$scope.queryMain();
		}
		$scope.ifFirstTimeQuery=false;//改变是否是第一次调用接口的状态
	})
	$scope.pageSize = 10;
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		if(response.code=="000000"){
			//$scope.pageSize = response.data.pageSize;
			$scope.list=response.data.data;
            $scope.totalCount = response.data.recordCount;
			angular.forEach($scope.list,function(data,index,array){
				if(data.taskId == "20033"){
					if(data.subject=="1"){
						data.subjectName="物理";
					}else if(data.subject=="2"){
						data.subjectName="美术";
					}else if(data.subject=="3"){
						data.subjectName="数学";
					}else if(data.subject=="4"){
						data.subjectName="体育";
					}else if(data.subject=="5"){
						data.subjectName="音乐";
					}else if(data.subject=="6"){
						data.subjectName="英语 ";
					}else if(data.subject=="7"){
						data.subjectName="语文";
					}else if(data.subject=="8"){
						data.subjectName="地理";
					}else if(data.subject=="9"){
						data.subjectName="化学";
					}else if(data.subject=="10"){
						data.subjectName="历史";
					}else if(data.subject=="11"){
						data.subjectName="信息技术";
					}else if(data.subject=="12"){
						data.subjectName="生物";
					}else if(data.subject=="13"){
						data.subjectName="政治";
					}else if(data.subject=="14"){
						data.subjectName="初中化学";
					}else if(data.subject=="15"){
						data.subjectName="初中生物";
					}else if(data.subject=="16"){
						data.subjectName="初中物理";
					}else if(data.subject=="17"){
						data.subjectName="高中化学";
					}else if(data.subject=="18"){
						data.subjectName="高中生物";
					}else if(data.subject=="19"){
						data.subjectName="高中物理";
					}
				}else if(data.taskId == "20037"){
					if(data.subject=="14"){
						data.subjectName="初中化学";
					}else if(data.subject=="15"){
						data.subjectName="初中生物";
					}else if(data.subject=="16"){
						data.subjectName="初中物理";
					}else if(data.subject=="17"){
						data.subjectName="高中化学";
					}else if(data.subject=="18"){
						data.subjectName="高中生物";
					}else if(data.subject=="19"){
						data.subjectName="高中物理";
					}
				}else if(data.taskId == "20038"){
					if(data.subject=="5"){
						data.subjectName="音乐";
					}else if(data.subject=="4"){
						data.subjectName="体育";
					}else if(data.subject=="2"){
						data.subjectName="美术";
					}
				}
				
				
				if(data.major=="0"){
					data.majorName="学前教育 ";
				}else if(data.major=="1"){
					data.majorName="小学教育";
				}else if(data.major=="2"){
					data.majorName="中学教育";
				}else{
					data.majorName="未选择";
				}
			})
			//recordCount=response.data.recordCount;				
		}else{
			alert(response.errorMsg);
		}
	}
	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */	
	//选择查询条件事件
	$scope.myFunc = function(){
		$scope.subject01="";
		$scope.taskId01="";
		$scope.major01="";
//		if($scope.sel=="0"){
//			$scope.queryAllTasks();
//			$scope.isSel01=false;
//			$scope.isSel02=true;
//			$scope.isSel03=true;
//		}else 
		if($scope.sel=="1"){
			//$scope.isSel01=true;
			$scope.isSel02=false;
			$scope.isSel03=true;
			$scope.isSel04=true;
		}else if($scope.sel=="2"){
			//$scope.isSel01=true;
			$scope.isSel02=true;
			$scope.isSel03=false;
			$scope.isSel04=true;
		}else{
			//$scope.isSel01=true;
			$scope.isSel02=true;
			$scope.isSel03=true;
			$scope.isSel04=true;
		} 
	}
	
	$scope.addM=function(){
		$scope.isAddM=false;
	}
	
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.clearInfo();
	}
	
	//上传数据
	$scope.uploadFile="";
	$scope.uploadFiles= function(file, errFiles,index) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        if (file) {
            file.upload = Upload.upload({
                url: apis.uploadExam,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                	if(response.data.code=="000000"){
                		$scope.fileName=file.name;
                		$scope.uploadFile=response.data.data;
                	}
                	else
                		alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                evt.loaded / evt.total));
            });
        }
    }
	
	//下载
	$scope.turnToDownLoad=function(pid){
		if(window.confirm("是否确定下载 ？")){
			location.href=apis.downloadExam+"?examId="+pid;
		}
	}
	
	$scope.showUrl=function(){
		if($scope.type=="2"){
			$scope.isUrl=false;
		}else{
			$scope.isUrl=true;
		}
	}
	
	//根据专业查询所有专业技能（选修+必修）
	$scope.queryAllTaskByMajor=function(){
		$scope.taskId01="";
		$http({
			method:'get',
			url:apis.queryTaskName,
			params:{'major':$scope.major01},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.taskList=response.data;
				$scope.isSel04=false;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了3，请稍等！");
		})
	}
	
	//根据必测还是选测查询专业技能
	$scope.queryTask=function(){
		$scope.isKemu = true;
		if($scope.isHExpert==true){//必测
			$scope.taskId="";
			$scope.querySpecialTask();
		}else{
			$scope.taskId00="";
			$scope.querySpecialTask1();
		}
	}
	
	//查询必测专业技能
	$scope.querySpecialTask=function(){
		$http({
			method:'get',
			url:apis.queryAuthSpecialTaskByType,
			params:{'major':$scope.majorId},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.selList=response.data;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了2，请稍等！");
		})
	}
	
	//查询选测测专业技能
	$scope.querySpecialTask1=function(){
		$http({
			method:'get',
			url:apis.queryAuthSpecialTaskByType1,
			params:{'major':$scope.majorId},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.xcList=response.data;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了1，请稍等！");
		})
	}
	
	//是否显示科目
	$scope.getRemark=function(){
		$scope.isKemu=true;
		var remark="";
		var task="";
		if($scope.isHExpert==true){
			remark=$scope.taskId;
		}else{
			remark=$scope.taskId00;
		}
		task=remark.split(",")[0]
		remark=remark.split(",")[1];
		if(remark=="1"){
			if(task=="20033"){//中学必修说课 显示科目
				$scope.isShuoke = true;
				$scope.isSkill = false;
			}else if(task=="20038"){
				$scope.isSkill = true;
				$scope.isShuoke = false;
			}
			$scope.isKemu=false;
		}
	}
	
	//删除
	$scope.delCollect=function(pid){
		if(window.confirm("是否确定删除？")){
			var rJson={'pid':pid};
			$http({
				method:'get',
				url:apis.deleteAuthExamInfo,
				params:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.pageSize = 10;
					$scope.queryMain();
				}else{
					alert(response.errorMsg);
				}
			})
		}
	}
	
	
	//添加
	$scope.addFirstZ=function(){
		var taskId="";
		if($scope.isHExpert==true){
			taskId=$scope.taskId;
		}else{
			taskId=$scope.taskId00;
		}		
		taskId=taskId.split(",")[0];
		if(window.confirm("是否确定添加？")){
			var rJson={'taskId':taskId,'subject':$scope.subject,'url':$scope.uploadFile,'major':$scope.majorId,'number':$scope.number};
			$http({
				method:'post',
				url:apis.saveAuthExamInfo,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.pageSize = 10;
					$scope.queryMain();
					$scope.clearInfo();
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				$scope.isAddM=true;
			})
		}
	}
	
	$scope.clearInfo=function(){
		$scope.number="";
		$scope.majorId="";
		$scope.taskId="";
		$scope.taskId00="";
		$scope.subject="";
		$scope.fileName="";
		$scope.uploadFile="";
		$scope.isKemu=true;
		$scope.isShuoke=false;
		$scope.isSkill=false;
	}
	//根据id查询机构信息
	/*$scope.queryAuthSchoolInfoById=function(pid){		
		$scope.isAddM=false;
		$scope.isclickBtn=true;
		$scope.Id=pid;
		var random=Math.random();
		$http({
			method:'get',
			url:apis.queryAuthSchoolInfoById,
			params:{'pid':pid,'random':random}
		}).success(function(response){
			if(response.code=="000000"){
				$scope.account=response.data.account;
				$scope.schoolName=response.data.schoolName;
				$scope.major=response.data.major;				
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}*/
	
	/*$scope.updateAuthSchoolInfoAccount=function(){
		if(window.confirm("是否确定修改？")){
			var rJson={'pid':$scope.Id,'account':$scope.account,'schoolname':$scope.schoolName,'major':$scope.major};
			$http({
				method:'post',
				url:apis.updateAuthSchoolInfoAccount,
				data:rJson,
			}).success(function(response){
				//var radom=Math.random();
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.queryMain();
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				$scope.isAddM=true;
			})
		}
	}*/
	
})

//分页controller
authApp.controller('pageDemo',function($scope){
	//定义分页的初始参数（不改动，不会影响功能）
	$scope.currentPage=1;
	$scope.itemsPerPage=10;
	$scope.maxSize = 8;
	//需要接口返回的一些参数，如pagecount
	$scope.$on('$getPageCount',function(event,data){
        $scope.totalItems = parseInt(data.recordCount);
        $scope.bigTotalItems =parseInt(data.recordCount);      
	});
	//分页点击的触发事件
	 $scope.$watch('currentPage', function() {
		 var pagedata={currentPage:$scope.currentPage};
		 $scope.$emit('$pageAction',pagedata);
	  });
})
